﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WpfApp_MvvmPatten.Base;

public class EventExtension
{

    public static ICommand GetEventBinding(DependencyObject obj)
    {
        return (ICommand)obj.GetValue(EventBindingProperty);
    }

    public static void SetEventBinding(DependencyObject obj, ICommand value)
    {
        obj.SetValue(EventBindingProperty, value);
    }

  
    public static readonly DependencyProperty EventBindingProperty =
        DependencyProperty.RegisterAttached("EventBinding", typeof(ICommand), typeof(EventExtension), new PropertyMetadata(null,new PropertyChangedCallback(EventBindingChangeEvent)));


    private static void EventBindingChangeEvent(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Button button = (Button)d;
        button.PreviewMouseLeftButtonDown += (s, e) => 
        {
            ICommand command = GetEventBinding(button);
            command.Execute(null);
        };
    }

}
